<?php

require_once 'Zend/Date.php';

class MHDEV_View_Helper_Datetime extends Zend_View_Helper_Abstract
{
    /**
     * @access private
     * @var Zend_Date
     */
    private $_date;
    
    public function __construct()
    {
        $this->_date 	= new Zend_Date();
		$auth			= Zend_Auth::getInstance();
		
        if($auth->hasIdentity())
		{
			$config		= Zend_Registry::get('config');
			$lang		= $auth->getIdentity()->language;
			$this->_date->setLocale($lang);
		}
    	else
		{
			$config		= Zend_Registry::get('config');
			$lang		= $config->default_language;
			$this->_date->setLocale($lang);
		}
    }
    
    /**
     * returns a nice datetime string
     *
     * @param string $datetime a datetime column string (YYYY-MM-DD HH:ii:ss)
     * @param string $format optional returned format
     * @param boolean $detailed
     * @param boolean $timestamp
     * 
     * @return string
     */
    public function datetime($datetime, $format = 'dd. MMMM yy', $detailed = FALSE, $timestamp = FALSE)
    {
    	$translate	= Zend_Registry::get('lang');
    	
        if($datetime == null)
            return 'Nie';
        
        $this->_date->set($datetime, Zend_Date::ISO_8601);
		
        if($detailed)
        {
	        if($this->_date->isToday()) {
	            return $translate->_('date_today') . $this->_date->toString('HH:m') . $translate->_('date_clock');
	        } elseif($this->_date->isYesterday()) {
	            return $translate->_('date_yesterday') . $this->_date->toString('HH:m') . $translate->_('date_clock');
	        } elseif($this->_date->isTomorrow()) {
	            return $translate->_('date_tomorrow') . $this->_date->toString('HH:m') . $translate->_('date_clock');
	        } else {
	            return $this->_date->toString($format);
	        }
        }
        elseif($timestamp)
        {
        	return $this->_date->getTimestamp();
        }
        
        return $this->_date->toString($format);
    }
} 